# 需要安装和调用的包
#install.packages("gt")
library(gt)
#install.packages("gtsummary")
library(gtsummary)
#install.packages("tidyverse")
library(tidyverse)
#install.package("labelled")
library(labelled)
{gt} package
常用函数
#基础用法
# 挑选mtcars中的4个变量,保留cyl == 6的行,按照wt排序
mycars <- mtcars %>%
select(mpg, cyl, disp, wt) %>%
filter(cyl == 6) %>%
arrange(wt)
mycars
#gt()显示的是变量标签label, 不是变量名称
mycars %>% gt()
| mpg |
cyl |
disp |
wt |
| 21.0 |
6 |
160.0 |
2.620 |
| 19.7 |
6 |
145.0 |
2.770 |
| 21.0 |
6 |
160.0 |
2.875 |
| 21.4 |
6 |
258.0 |
3.215 |
| 19.2 |
6 |
167.6 |
3.440 |
| 17.8 |
6 |
167.6 |
3.440 |
| 18.1 |
6 |
225.0 |
3.460 |
cols_label()
#cols_label()修改变量标签, tab_header()修改表格标题
mycars_table <- mycars %>%
gt() %>%
cols_label(
mpg = "Miles per Gallon",
cyl = "Number of Cylinders",
disp = "Displacement (cu.in.)",
wt = "Weight (1000 lbs)"
)
mycars_table
| Miles per Gallon |
Number of Cylinders |
Displacement (cu.in.) |
Weight (1000 lbs) |
| 21.0 |
6 |
160.0 |
2.620 |
| 19.7 |
6 |
145.0 |
2.770 |
| 21.0 |
6 |
160.0 |
2.875 |
| 21.4 |
6 |
258.0 |
3.215 |
| 19.2 |
6 |
167.6 |
3.440 |
| 17.8 |
6 |
167.6 |
3.440 |
| 18.1 |
6 |
225.0 |
3.460 |
tab_options()
mycars_table <- mycars_table %>%
tab_options(
data_row.padding = px(6), # 行高
heading.align = 'left', # 标题对齐方式
column_labels.background.color = 'dodgerblue4', # 列标签背景颜色
heading.title.font.size = px(26), # 标题字体大小
heading.subtitle.font.size = px(14), # 副标题字体大小
table_body.hlines.width = px(3) # 表格内部横线宽度
)
mycars_table
| 1974 Motor Trend US Magazine Data |
| Cars with 6 Cylinders |
| Miles per Gallon |
Number of Cylinders |
Displacement (cu.in.) |
Weight (1000 lbs) |
| 21.0 |
6 |
160.0 |
2.620 |
| 19.7 |
6 |
145.0 |
2.770 |
| 21.0 |
6 |
160.0 |
2.875 |
| 21.4 |
6 |
258.0 |
3.215 |
| 19.2 |
6 |
167.6 |
3.440 |
| 17.8 |
6 |
167.6 |
3.440 |
| 18.1 |
6 |
225.0 |
3.460 |
tab_style()
mycars_table <- mycars_table %>%
tab_style(
style = cell_text(
color = "dodgerblue4", # 字体颜色
weight = "bold", # 字体加粗
font = google_font("Open Sans") #查找google字体https://fonts.google.com/
),
locations = cells_title(groups = "title") #locations设置样式应用的位置,应用到标题行
) %>%
tab_style(
style = cell_fill(color = 'grey90'), # 设置单元格背景颜色
locations = cells_body(rows = seq(1, nrow(mycars), 2)) #locations设置样式应用应用到标题行
)
mycars_table
| 1974 Motor Trend US Magazine Data |
| Cars with 6 Cylinders |
| Miles per Gallon |
Number of Cylinders |
Displacement (cu.in.) |
Weight (1000 lbs) |
| 21.0 |
6 |
160.0 |
2.620 |
| 19.7 |
6 |
145.0 |
2.770 |
| 21.0 |
6 |
160.0 |
2.875 |
| 21.4 |
6 |
258.0 |
3.215 |
| 19.2 |
6 |
167.6 |
3.440 |
| 17.8 |
6 |
167.6 |
3.440 |
| 18.1 |
6 |
225.0 |
3.460 |
创建主题函数
#创建函数的简单例子:求圆的面积的函数
circle_area <- function(r) {
pi * r^2
}
circle_area(1)
[1] 3.141593
[1] 12.56637
#创建一个自定义主题函数
mygt_theme <- function(gt_tbl) {
gt_tbl %>%
tab_options(
data_row.padding = px(6), # 行高
heading.align = 'left', # 标题对齐方式
column_labels.background.color = 'dodgerblue4', # 列标签背景颜色
heading.title.font.size = px(26), # 标题字体大小
heading.subtitle.font.size = px(14), # 副标题字体大小
table_body.hlines.width = px(3) # 表格内部横线宽度
) %>%
tab_style(
style = cell_text(
color = "dodgerblue4", # 字体颜色
weight = "bold", # 字体加粗
font = google_font("Open Sans") #查找google字体https://fonts.google.com/
),
locations = cells_title(groups = "title") #locations设置样式应用的位置,应用到标题行
)
}
应用主题函数+其他细节设置
#将iris转换为gt对象,然后使用mygt_theme()函数,适用自定义主题
iris_table <- iris %>%
head() %>%
gt() %>%
mygt_theme() %>% #应用自定义主题函数
tab_header(
title = "Fisher's Iris Data",
subtitle = "ris setosa, versicolor, and virginica"
) %>%
tab_style(
style = cell_text(
color = "dodgerblue4", # 字体颜色
weight = "bold", # 字体加粗
font = google_font("Open Sans") #查找google字体https://fonts.google.com/
),
locations = cells_title(groups = "title") #locations设置样式应用的位置,应用到标题行
)
iris_table
| Fisher's Iris Data |
| ris setosa, versicolor, and virginica |
| Sepal.Length |
Sepal.Width |
Petal.Length |
Petal.Width |
Species |
| 5.1 |
3.5 |
1.4 |
0.2 |
setosa |
| 4.9 |
3.0 |
1.4 |
0.2 |
setosa |
| 4.7 |
3.2 |
1.3 |
0.2 |
setosa |
| 4.6 |
3.1 |
1.5 |
0.2 |
setosa |
| 5.0 |
3.6 |
1.4 |
0.2 |
setosa |
| 5.4 |
3.9 |
1.7 |
0.4 |
setosa |
高级:创建依赖于数据的主题函数
#提取表中的数据框
iris_table$`_data`
#表中数据框的行数
nrow(iris_table$`_data`)
[1] 6
mygt_theme_grey_row <- function(gt_tbl) {
n_rows <- nrow(gt_tbl$`_data`) # 获取表格行数
gt_tbl %>%
tab_options(
data_row.padding = px(6), # 行高
heading.align = 'left', # 标题对齐方式
column_labels.background.color = 'dodgerblue4', # 列标签背景颜色
heading.title.font.size = px(26), # 标题字体大小
heading.subtitle.font.size = px(14), # 副标题字体大小
table_body.hlines.width = px(3) # 表格内部横线宽度
) %>%
tab_style(
style = cell_text(
color = "dodgerblue4", # 字体颜色
weight = "bold", # 字体加粗
font = google_font("Open Sans") #查找google字体https://fonts.google.com/
),
locations = cells_title(groups = "title") #locations设置样式应用的位置,应用到标题行
) %>%
tab_style(
style = cell_fill(color = 'grey90'), # 设置单元格背景颜色
locations = cells_body(rows = seq(1, n_rows, 2)) #n_rows代表数据框的行数
)
}
iris_table %>% mygt_theme_grey_row()
| Fisher's Iris Data |
| ris setosa, versicolor, and virginica |
| Sepal.Length |
Sepal.Width |
Petal.Length |
Petal.Width |
Species |
| 5.1 |
3.5 |
1.4 |
0.2 |
setosa |
| 4.9 |
3.0 |
1.4 |
0.2 |
setosa |
| 4.7 |
3.2 |
1.3 |
0.2 |
setosa |
| 4.6 |
3.1 |
1.5 |
0.2 |
setosa |
| 5.0 |
3.6 |
1.4 |
0.2 |
setosa |
| 5.4 |
3.9 |
1.7 |
0.4 |
setosa |
ggplot2::mpg %>%
filter(class == "2seater") %>%
gt() %>%
mygt_theme_grey_row()
| manufacturer |
model |
displ |
year |
cyl |
trans |
drv |
cty |
hwy |
fl |
class |
| chevrolet |
corvette |
5.7 |
1999 |
8 |
manual(m6) |
r |
16 |
26 |
p |
2seater |
| chevrolet |
corvette |
5.7 |
1999 |
8 |
auto(l4) |
r |
15 |
23 |
p |
2seater |
| chevrolet |
corvette |
6.2 |
2008 |
8 |
manual(m6) |
r |
16 |
26 |
p |
2seater |
| chevrolet |
corvette |
6.2 |
2008 |
8 |
auto(s6) |
r |
15 |
25 |
p |
2seater |
| chevrolet |
corvette |
7.0 |
2008 |
8 |
manual(m6) |
r |
15 |
24 |
p |
2seater |
常见报错
# table不能导入gt()函数
# table(mtcars$cyl, mtcars$am) %>%
# gt()
table(mtcars$cyl, mtcars$am) %>%
str()
'table' int [1:3, 1:2] 3 4 12 8 3 2
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "4" "6" "8"
..$ : chr [1:2] "0" "1"
#正确
table(mtcars$cyl, mtcars$am) %>%
as.data.frame() %>%
gt()
| Var1 |
Var2 |
Freq |
| 4 |
0 |
3 |
| 6 |
0 |
4 |
| 8 |
0 |
12 |
| 4 |
1 |
8 |
| 6 |
1 |
3 |
| 8 |
1 |
2 |
mat <- matrix(1:12, nrow = 3, ncol = 4,
dimnames = list(letters[1:3], LETTERS[1:4]))
# 报错:矩阵不能导入gt()函数
# mat %>% gt()
mat %>%
as.data.frame() %>%
gt()
| A |
B |
C |
D |
| 1 |
4 |
7 |
10 |
| 2 |
5 |
8 |
11 |
| 3 |
6 |
9 |
12 |
{gtsummary}
package
tbl_summary()函数
library(gtsummary)
library(tidyverse)
head(trial)
| Chemotherapy Treatment |
Age |
Marker Level (ng/mL) |
T Stage |
Grade |
Tumor Response |
Patient Died |
Months to Death/Censor |
| Drug A |
23 |
0.160 |
T1 |
II |
0 |
0 |
24.00 |
| Drug B |
9 |
1.107 |
T2 |
I |
1 |
0 |
24.00 |
| Drug A |
31 |
0.277 |
T1 |
II |
0 |
0 |
24.00 |
| Drug A |
NA |
2.067 |
T3 |
III |
1 |
1 |
17.64 |
| Drug A |
51 |
2.767 |
T4 |
III |
1 |
1 |
16.43 |
| Drug B |
39 |
0.613 |
T4 |
I |
0 |
1 |
15.64 |
tibble [200 × 8] (S3: tbl_df/tbl/data.frame)
$ trt : chr [1:200] "Drug A" "Drug B" "Drug A" "Drug A" ...
..- attr(*, "label")= chr "Chemotherapy Treatment"
$ age : num [1:200] 23 9 31 NA 51 39 37 32 31 34 ...
..- attr(*, "label")= chr "Age"
$ marker : num [1:200] 0.16 1.107 0.277 2.067 2.767 ...
..- attr(*, "label")= chr "Marker Level (ng/mL)"
$ stage : Factor w/ 4 levels "T1","T2","T3",..: 1 2 1 3 4 4 1 1 1 3 ...
..- attr(*, "label")= chr "T Stage"
$ grade : Factor w/ 3 levels "I","II","III": 2 1 2 3 3 1 2 1 2 1 ...
..- attr(*, "label")= chr "Grade"
$ response: int [1:200] 0 1 0 1 1 0 0 0 0 0 ...
..- attr(*, "label")= chr "Tumor Response"
$ death : int [1:200] 0 0 0 1 1 1 0 1 0 1 ...
..- attr(*, "label")= chr "Patient Died"
$ ttdeath : num [1:200] 24 24 24 17.6 16.4 ...
..- attr(*, "label")= chr "Months to Death/Censor"
#创建sm_trial数据框
sm_trial <-
trial %>%
select(trt, age, grade, response)
#报告sm_trial数据框的summary
sm_trial %>%
select(-trt) %>%
tbl_summary()
| Characteristic |
N = 200 |
| Age |
47 (38, 57) |
| Unknown |
11 |
| Grade |
|
| I |
68 (34%) |
| II |
68 (34%) |
| III |
64 (32%) |
| Tumor Response |
61 (32%) |
| Unknown |
7 |
#按trt分组后,报告summary
sm_trial %>%
tbl_summary(by = trt) #指定分组变量
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
46 (37, 59) |
48 (39, 56) |
| Unknown |
7 |
4 |
| Grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
28 (29%) |
33 (34%) |
| Unknown |
3 |
4 |
type =
设置变量的summary type
sm_trial %>%
tbl_summary(
by = trt,
type = age ~ "continuous2")
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
|
|
| Median (IQR) |
46 (37, 59) |
48 (39, 56) |
| Unknown |
7 |
4 |
| Grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
28 (29%) |
33 (34%) |
| Unknown |
3 |
4 |
sm_trial %>%
tbl_summary(
by = trt,
type = response ~ "categorical")
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
46 (37, 59) |
48 (39, 56) |
| Unknown |
7 |
4 |
| Grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
|
|
| 0 |
67 (71%) |
65 (66%) |
| 1 |
28 (29%) |
33 (34%) |
| Unknown |
3 |
4 |
statistics =
自定义要报告的统计量
sm_trial %>%
tbl_summary(
by = trt,
type = age ~ "continuous2",
statistic =
list(age ~ c("{mean} ({sd})", #均值和标准差放在一行
"{min}, {max}"), #最小值和最大值放在一行
response ~ "{n} / {N} ({p}%)"
)
)
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
|
|
| Mean (SD) |
47 (15) |
47 (14) |
| Range |
6, 78 |
9, 83 |
| Unknown |
7 |
4 |
| Grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
28 / 95 (29%) |
33 / 98 (34%) |
| Unknown |
3 |
4 |
trial %>%
tbl_summary(
by = trt,
statistic =
list(all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)"
)
)
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
47 (15) |
47 (14) |
| Unknown |
7 |
4 |
| Marker Level (ng/mL) |
1.02 (0.89) |
0.82 (0.83) |
| Unknown |
6 |
4 |
| T Stage |
|
|
| T1 |
28 (29%) |
25 (25%) |
| T2 |
25 (26%) |
29 (28%) |
| T3 |
22 (22%) |
21 (21%) |
| T4 |
23 (23%) |
27 (26%) |
| Grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
28 (29%) |
33 (34%) |
| Unknown |
3 |
4 |
| Patient Died |
52 (53%) |
60 (59%) |
| Months to Death/Censor |
20.2 (5.0) |
19.0 (5.5) |
label =
修改标签
sm_trial %>%
tbl_summary(
by = trt,
type = age ~ "continuous2",
statistic =
list(age ~ c("{mean} ({sd})",
"{min}, {max}"),
response ~ "{n} / {N} ({p}%)"
),
label = grade ~ "Pathologic tumor grade"
)
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
|
|
| Mean (SD) |
47 (15) |
47 (14) |
| Range |
6, 78 |
9, 83 |
| Unknown |
7 |
4 |
| Pathologic tumor grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
28 / 95 (29%) |
33 / 98 (34%) |
| Unknown |
3 |
4 |
设置小数位数
sm_trial %>%
tbl_summary(
by = trt,
type = age ~ "continuous2",
statistic =
list(age ~ c("{mean} ({sd})",
"{min}, {max}"),
response ~ "{n} / {N} ({p}%)"
),
label = grade ~ "Pathologic tumor grade",
digits = age ~ 1
)
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
|
|
| Mean (SD) |
47.0 (14.7) |
47.4 (14.0) |
| Range |
6.0, 78.0 |
9.0, 83.0 |
| Unknown |
7 |
4 |
| Pathologic tumor grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
28 / 95 (29%) |
33 / 98 (34%) |
| Unknown |
3 |
4 |
add_函数
add_overall()函数
sm_trial %>%
tbl_summary(
by = trt
) %>%
add_overall()
| Characteristic |
Overall, N = 200 |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
47 (38, 57) |
46 (37, 59) |
48 (39, 56) |
| Unknown |
11 |
7 |
4 |
| Grade |
|
|
|
| I |
68 (34%) |
35 (36%) |
33 (32%) |
| II |
68 (34%) |
32 (33%) |
36 (35%) |
| III |
64 (32%) |
31 (32%) |
33 (32%) |
| Tumor Response |
61 (32%) |
28 (29%) |
33 (34%) |
| Unknown |
7 |
3 |
4 |
add_n()函数
sm_trial %>%
tbl_summary(
by = trt
) %>%
add_overall() %>%
add_n()
| Characteristic |
N |
Overall, N = 200 |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
189 |
47 (38, 57) |
46 (37, 59) |
48 (39, 56) |
| Unknown |
|
11 |
7 |
4 |
| Grade |
200 |
|
|
|
| I |
|
68 (34%) |
35 (36%) |
33 (32%) |
| II |
|
68 (34%) |
32 (33%) |
36 (35%) |
| III |
|
64 (32%) |
31 (32%) |
33 (32%) |
| Tumor Response |
193 |
61 (32%) |
28 (29%) |
33 (34%) |
| Unknown |
|
7 |
3 |
4 |
add_stat_label()函数
sm_trial %>%
tbl_summary(
by = trt,
missing = "no"
) %>%
add_overall() %>%
add_n() %>%
add_stat_label(
label = all_categorical() ~ "No.(%)"
)
| Characteristic |
N |
Overall, N = 200 |
Drug A, N = 98 |
Drug B, N = 102 |
| Age, Median (IQR) |
189 |
47 (38, 57) |
46 (37, 59) |
48 (39, 56) |
| Grade, No.(%) |
200 |
|
|
|
| I |
|
68 (34%) |
35 (36%) |
33 (32%) |
| II |
|
68 (34%) |
32 (33%) |
36 (35%) |
| III |
|
64 (32%) |
31 (32%) |
33 (32%) |
| Tumor Response, No.(%) |
193 |
61 (32%) |
28 (29%) |
33 (34%) |
add_p()函数
sm_trial %>%
tbl_summary(
by = trt
) %>%
add_p()
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
46 (37, 59) |
48 (39, 56) |
0.7 |
| Unknown |
7 |
4 |
|
| Grade |
|
|
0.9 |
| I |
35 (36%) |
33 (32%) |
|
| II |
32 (33%) |
36 (35%) |
|
| III |
31 (32%) |
33 (32%) |
|
| Tumor Response |
28 (29%) |
33 (34%) |
0.5 |
| Unknown |
3 |
4 |
|
sm_trial %>%
tbl_summary(
by = trt
) %>%
add_p(age ~ "t.test",
test.args = age ~ list(var.equal = TRUE))
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
46 (37, 59) |
48 (39, 56) |
0.8 |
| Unknown |
7 |
4 |
|
| Grade |
|
|
0.9 |
| I |
35 (36%) |
33 (32%) |
|
| II |
32 (33%) |
36 (35%) |
|
| III |
31 (32%) |
33 (32%) |
|
| Tumor Response |
28 (29%) |
33 (34%) |
0.5 |
| Unknown |
3 |
4 |
|
bold_p()函数
bold_labels()函数
sm_trial %>%
tbl_summary(
by = trt
) %>%
add_p() %>%
bold_p(t = 0.8) %>% #p值小于0.8的加粗,仅作示范用,默认t = 0.05
bold_labels() %>%
italicize_levels()
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
46 (37, 59) |
48 (39, 56) |
0.7 |
| Unknown |
7 |
4 |
|
| Grade |
|
|
0.9 |
| I |
35 (36%) |
33 (32%) |
|
| II |
32 (33%) |
36 (35%) |
|
| III |
31 (32%) |
33 (32%) |
|
| Tumor Response |
28 (29%) |
33 (34%) |
0.5 |
| Unknown |
3 |
4 |
|
add_difference()函数
trial %>%
select(trt, age, marker, response) %>%
tbl_summary(
by = trt,
statistic = list(all_continuous() ~ "{mean} ({sd})",
response ~ "{p}"),
digits = all_continuous() ~ 2,
missing = "no"
) %>%
add_difference()
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
Difference |
95% CI |
p-value |
| Age |
47.01 (14.71) |
47.45 (14.01) |
-0.44 |
-4.6, 3.7 |
0.8 |
| Marker Level (ng/mL) |
1.02 (0.89) |
0.82 (0.83) |
0.20 |
-0.05, 0.44 |
0.12 |
| Tumor Response |
29 |
34 |
-4.2% |
-18%, 9.9% |
0.6 |
modify_函数
tbl_cross()
sm_trial %>%
tbl_cross(
row = trt,
col = response,
percent = "row", #计算行百分比
margin = "row" #计算行总数
)
|
Tumor Response
|
| 0 |
1 |
Unknown |
| Chemotherapy Treatment |
|
|
|
| Drug A |
67 (68%) |
28 (29%) |
3 (3.1%) |
| Drug B |
65 (64%) |
33 (32%) |
4 (3.9%) |
| Total |
132 (66%) |
61 (31%) |
7 (3.5%) |
sm_trial %>%
tbl_cross(
row = trt,
col = response,
percent = "row",
) %>%
add_p(source_note = TRUE) %>%
bold_labels()
|
Tumor Response
|
Total |
| 0 |
1 |
Unknown |
| Chemotherapy Treatment |
|
|
|
|
| Drug A |
67 (68%) |
28 (29%) |
3 (3.1%) |
98 (100%) |
| Drug B |
65 (64%) |
33 (32%) |
4 (3.9%) |
102 (100%) |
| Total |
132 (66%) |
61 (31%) |
7 (3.5%) |
200 (100%) |
| Fisher’s exact test, p=0.7 |
tbl_continuous()
sm_trial %>%
tbl_continuous(
variable = age,
by = trt,
include = grade
)
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Grade |
|
|
| I |
46 (36, 60) |
48 (42, 55) |
| II |
45 (31, 55) |
51 (43, 57) |
| III |
52 (42, 60) |
45 (36, 52) |
tbl_strata()
sm_trial %>%
mutate(grade = paste("Grade", grade)) %>%
tbl_strata(
strata = grade, #设置分层变量
~ tbl_summary(., by = trt, missing = "no") %>% #.占位符,代表pipe进来的数据
modify_header(all_stat_cols() ~ "**{level}**")
)
| Characteristic |
Grade I
|
Grade II
|
Grade III
|
| Drug A |
Drug B |
Drug A |
Drug B |
Drug A |
Drug B |
| Age |
46 (36, 60) |
48 (42, 55) |
45 (31, 55) |
51 (43, 57) |
52 (42, 60) |
45 (36, 52) |
| Tumor Response |
8 (23%) |
13 (41%) |
7 (23%) |
12 (36%) |
13 (43%) |
8 (24%) |
主题函数
reset_gtsummary_theme() #重置主题
theme_gtsummary_journal()
theme_gtsummary_language()
reset_gtsummary_theme() #重置主题
sm_trial %>%
tbl_summary() %>%
modify_caption("Default Theme")
Default Theme
| Characteristic |
N = 200 |
| Chemotherapy Treatment |
|
| Drug A |
98 (49%) |
| Drug B |
102 (51%) |
| Age |
47 (38, 57) |
| Unknown |
11 |
| Grade |
|
| I |
68 (34%) |
| II |
68 (34%) |
| III |
64 (32%) |
| Tumor Response |
61 (32%) |
| Unknown |
7 |
theme_gtsummary_journal(journal = "jama") #设置主题
sm_trial %>%
tbl_summary() %>%
modify_caption("Journal Theme (JAMA)")
Journal Theme (JAMA)
| Characteristic |
N = 200 |
| Chemotherapy Treatment, n (%) |
|
| Drug A |
98 (49) |
| Drug B |
102 (51) |
| Age, Median (IQR) |
47 (38 – 57) |
| Unknown |
11 |
| Grade, n (%) |
|
| I |
68 (34) |
| II |
68 (34) |
| III |
64 (32) |
| Tumor Response, n (%) |
61 (32) |
| Unknown |
7 |
theme_gtsummary_language(lang = "zh-cn") #简体中文
sm_trial %>%
tbl_summary() %>%
modify_caption("Language Theme (Chinese)")
Language Theme (Chinese)
| 特征 |
N = 200 |
| Chemotherapy Treatment, n (%) |
|
| Drug A |
98 (49) |
| Drug B |
102 (51) |
| Age, 中位数 (四分位距) |
47 (38 – 57) |
| 未知数 |
11 |
| Grade, n (%) |
|
| I |
68 (34) |
| II |
68 (34) |
| III |
64 (32) |
| Tumor Response, n (%) |
61 (32) |
| 未知数 |
7 |
theme_gtsummary_language(lang = "zh-tw") #繁体中文
sm_trial %>%
tbl_summary() %>%
modify_caption("Language Theme (Chinese Traditional)")
Language Theme (Chinese Traditional)
| 特色 |
N = 200 |
| Chemotherapy Treatment, n (%) |
|
| Drug A |
98 (49) |
| Drug B |
102 (51) |
| Age, 中位數 (四分位距) |
47 (38 – 57) |
| 未知 |
11 |
| Grade, n (%) |
|
| I |
68 (34) |
| II |
68 (34) |
| III |
64 (32) |
| Tumor Response, n (%) |
61 (32) |
| 未知 |
7 |